# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libicsneo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
pkgver=0.2.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
pkgdesc="The Intrepid Control Systems Open Cross-Platform Device Communication API (mingw-w64)"
license=("custom")
url="https://github.com/intrepidcs/libicsneo"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' '!debug' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/intrepidcs/libicsneo/archive/v${pkgver}.tar.gz
        001-mingw-build.patch
        002-install-targets.patch)
sha256sums=('19f90436e050ba1dfa025f02470a9ffda2b7c117ffe46f01e19a4f139451099d'
            'fe44449cdd96d59a45d8a777acf0e2d0a99ca6b377de1f508165a5822e6f3551'
            '9dec215bc7e39ae24843428d9b21f4d0f76d39cfb1021bd29f332530cd7a89b6')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-mingw-build.patch \
    002-install-targets.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
